// pages/candidate-detail/candidate-detail.js
import { Candidate } from '../../models/candidate'
import Call from '../../util/call'
import { Collect } from '../../models/collect'
import { Resume } from '../../models/resume'
import queryParams from '../../util/query'
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog'
const resumeModel = new Resume()
const candidateModel = new Candidate()
const collectModel = new Collect()

const app = getApp()
Page({
  data: {
    data: {},
    id: '',
    isIphoneX: app.globalData.isIPhoneX,
    recruitList: [],
    recruitStatus: 0, // 0: 什么都没发布过 1: 有通过 2: 全部拒绝 部分通过的就不考虑了
    showCheckRecruit: false,
    selectedRecruit: '',
    from: '',
    showBackHome: false,
    showDialog: false,
    resumeUserId: ''
  },

  onLoad(options) {
    wx.showLoading({
      title: '加载中'
    })
    this.setData({
      id: options.id || queryParams(decodeURIComponent(options.q), 'id'),
      from: options.from || '',
      showBackHome: !!options.ald_share_src
    })
    this.getDetail()
  },

  getDetail() {
    candidateModel.detail({
      id: this.data.id
    }, res => {
      if (res.auditStatus == 0) {
        // setTimeout(() => {
        //   Notify({
        //     message: '正在审核中，请耐心等待回复！',
        //     duration: 30000,
        //     background: '#46E3BC'
        //   })
        // }, 1000)
        Dialog.alert({
          message: '正在审核中，请耐心等待回复！',
        }).then(() => {
          // on close
        })
      }
      wx.hideLoading()
      this.setData({
        data: res
      })
    })
  },

  toNewPage(event) {
    this.setData({
      showDialog: false
    })
    let path = event.currentTarget.dataset.path
    wx.navigateTo({
      url: `/pages/${path}/${path}`
    })
  },

  cancelDialog() {
    this.setData({
      showDialog: false
    })
  },

  getRecruit() {
    return new Promise((resolve, reject) => {
      collectModel.myPublish({
        type: '招聘'
      }, res => {
        if (res.rows.length) {
          let statusArray = []
          res.rows.forEach(item => {
            if (item.auditStatus == 0 || item.auditStatus == 2 || item.auditStatus == 3) {
              statusArray.push(2)
            }
            if (item.auditStatus == 1) {
              statusArray.push(1)
            }
          })
          if (statusArray.includes(2)) {
            this.setData({
              recruitStatus: 2
            })
          }
          if (statusArray.includes(1)) {
            this.setData({
              recruitStatus: 1
            })
          }
          this.setData({
            recruitList: res.rows.filter(item => item.auditStatus == 1)
          })
        }
        resolve(true)
      })
    })
  },

  async delivery() {
    // 投递信息
    if (!wx.getStorageSync('userToken')) {
      wx.showToast({
        title: '请先登录',
        icon: 'none'
      })
      wx.navigateTo({
        url: `/pages/login/login`
      })
      return
    }
    await this.getRecruit()
    switch (this.data.recruitStatus) {
      case 0:
        this.setData({
          showDialog: true
        })
        break
      case 1:
        this.setData({
          showCheckRecruit: true
        })
        break
      case 2:
        wx.showToast({
          title: '请耐心等待招聘审核通过后投递！',
          icon: 'none'
        })
        break
    }
  },

  onCheckedReciurt(e) {
    const id = e.currentTarget.dataset.item.id
    this.setData({
      selectedRecruit: id
    })
  },

  confirmRecruit() {
    this.setData({
      showCheckRecruit: false
    })
    resumeModel.delivery({
      recruitId: this.data.selectedRecruit,
      resumeId: this.data.data.id,
      status: 1
    }, res => {
      wx.showToast({
        title: res,
        icon: 'none'
      })
      wx.requestSubscribeMessage({
        tmplIds: ['6E4h4hY0mRpdOPOZLZj6EGV26CKx-3BoSJ3wwLcs3t4', 'gqiUsZicpSK12MajZO_xtAITyrFoFgw4A1n6fibfN0w', 'eyzGwf2pUMpnz03Hq27JbBefHa6FLYXIgx8evLxMZlE'],
        complete(res) {
        }
      })
    })
  },

  cancelRecruit() {
    this.setData({
      selectedRecruit: '',
      showCheckRecruit: false
    })
  },

  checkCall(e) {
    this.setData({
      resumeUserId: e.currentTarget.dataset.userid
    })
    resumeModel.checkCall({
      resumeId: this.data.data.id
    }, res => {
      this.call({
        resumeUserId: this.data.resumeUserId,
        resumeId: this.data.data.id,
        recruitUserId: wx.getStorageSync('userInfo').user_id,
      })
    })
  },

  call(markInfo) {
    // 拨打电话
    Call(this.data.id, '求职', markInfo)
    // wx.requestSubscribeMessage({
    //   tmplIds: ['6E4h4hY0mRpdOPOZLZj6EGV26CKx-3BoSJ3wwLcs3t4', 'gqiUsZicpSK12MajZO_xtLfDLXEjyvcTp51cs7GsqoY', '-DHKrW9DRvbg3sEoVRYtcV92AWETa-B9VW8Wst3WgMg'],
    // })
  },
  onReady() {
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '求职' + this.data.data.title,
      path: `/pages/candidate-detail/candidate-detail?id=${this.data.id}&otherId=${wx.getStorageSync('userInfo').user_id}&time=${+new Date()}`
    }
  }
})
